import {reactive} from "vue";
import api from "@/api/api";
import type {
    Background,
    Banner,
    CompanyLetter,
    Goods,
    Recommend,
    SplitSection,
    SplitSelect
} from "@/Types/HomeTypes";





class HomeData {
    flowBanners:Banner[] = []
    hotAd?:Banner
    metro:Banner[] = []
    splistBanner: SplitSection[] = []
    splitSelect: SplitSelect[] = []
    companyLetters:CompanyLetter[] = []
    recommend:Recommend[] = []
    recommendSelectIndex:number = 0
    background?:Background
}
export const homeData = reactive(new HomeData());
export  function initData(){
    api.homeApi.flowBanner<Banner[]>({
        storeId:"2000001394",
        userType:"10"
    }).then((result)=>{
        homeData.flowBanners = result.data ?? []
    })
    api.homeApi.hotAd<Banner>({
        storeId:"2000001394",
        userType:"10"
    }).then((result)=>{
        homeData.hotAd = result.data
    })
    api.homeApi.metro<Banner[]>({
        storeId:"2000001394",
        userType:"10"
    }).then((result)=>{
       homeData.metro = result.data ?? []
   })
    api.homeApi.splitBanner<SplitSection[]>({
        storeId:"2000001394",
        userType:"10"
    }).then((result)=>{
        homeData.splistBanner = result.data ?? []
    })
    api.homeApi.selectSpecialAd<SplitSelect[]>({
        storeId:"2000001394",
        userType:"10"
    }).then((result)=>{
        homeData.splitSelect = result.data ?? []
    })
    api.homeApi.companyLetter<CompanyLetter[]>({
        storeId:"2000001394",
        userType:"10"
    }).then((result)=>{
        homeData.companyLetters = result.data ?? []
    })
    api.homeApi.recommend<Recommend[]>({
        storeId:"2000001394",
        userType:"10"
    }).then((result)=>{
        homeData.recommend = result.data ?? []
        for (let i=0;i<homeData.recommend.length;i++) {
            const recommend = homeData.recommend[i]
            recommend.pages = 1
            recommend.size = 10
            api.homeApi.recommendQueryPage<GoodsRespose>({
                storeId: "2000001394",
                id: recommend.id.toString(),
                curNum: "1",
                offset: "10"
            }).then((reslut)=>{
                const records = reslut.data?.records ?? []
                recommend.homeRecommendProductVOList.push(...records)
            })
        }
    })
    api.homeApi.background<Background>({
        storeId:"2000001394",
        userType:"10"
    }).then((result)=>{
        homeData.background = result.data
    })
}
type GoodsRespose = {
    records:Goods[]
}
